import { message } from 'antd';
import { fetchQueryTable,handleDelate,getToken,userTest } from '../services/api';
import { routerRedux } from 'dva/router';
import cookie from "react-cookies";
import {stringify} from "qs";

const OrderModel = {
  namespace: 'account',
  state: {
    pageInfo: {},
    code: null,
  },
  effects: {

    // 获取列表
    * fetchQueryTable({ payload }, { call, put }) {
      // const response = yield call(fetchQueryTable, payload);
      const response = {
        code:'0',
        result:{
          list:[
            {
              accountName: "lisansui",
              tel: 18225738098,
              accountType: "company",
              companyId: null,
              companyName: "阿尔农业股份有限公司",
              createUseId: 93,
              createUserName: "root",
              deleted: 0,
              empRealName: "李三岁",
              gmtCreate: 1559203355000,
              gmtModified: 1559209076000,
              id: 197,
              modifyUserId: null,
              modifyUserName: null,
              password: null,
              stauts: 0,
              userId: 291,
            },
            {
              accountName: "lisansui",
              tel: 18225738098,
              accountType: "company",
              companyId: null,
              companyName: "阿尔农业股份有限公司",
              createUseId: 93,
              createUserName: "root",
              deleted: 0,
              empRealName: "李三岁",
              gmtCreate: 1559203355000,
              gmtModified: 1559209076000,
              id: 198,
              modifyUserId: null,
              modifyUserName: null,
              password: null,
              stauts: 0,
              userId: 291,
            },
          ],
          total:2
        }
      }
      if (response.code !== '0') {
        message.error(response.msg);
      }
      yield put({
        type: 'updateState',
        payload: {
          pageInfo: response.result,
        },
      });
    },

    // 删除
    * handleDelate({ payload }, { call, put }) {
      // const response = yield call(handleDelate, payload);
      const response = {
        code:'0',
        result:1
      }
      if (response.code !== '0') {
        message.error(response.msg);
      }
      yield put({
        type: 'updateState',
        payload: {
          code: response.result,
        },
      });
    },
    // 获取登陆token
    * getToken({ payload }, { call, put }) {
      const response = yield call(getToken,payload);
      console.log("获取token",response);
    },
    // 测试token
    * userTest({ payload }, { call, put }) {
      const response = yield call(userTest,payload);
      console.log("测试token",response);
      if(response && response.requestStatus === 401){
        cookie.remove("user_id",{ path: '/' });
        cookie.remove("access_token",{ path: '/' });
        yield put(
          routerRedux.replace({
            pathname: '/user/login',
          }),
        );
      }
    },
  },
  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

  },
};
export default OrderModel;
